package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.mongodb.MongoClientURI;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :ProductBrandInfoWapper.java
 * @Author :ligaoge
 * @Date : 2016年8月23日
 * @Copyright : 洋码头 商品合库 新库
 ************************************************************************************/
public class BuyerProductWapper {
	MongoDBDriver mdd;

	public BuyerProductWapper() throws NumberFormatException, Exception {
		MongoClientURI uri = new MongoClientURI(EnvSetup.getSystemUrl("set5_mongo_YmtBuyerProduct"));
		mdd = new MongoDBDriver(uri);
	}
	public void close() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		mdd.close();
	}
	/**
	 * 按BrandId查询出一条记录
	 * 
	 * @param BrandId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductBrandInfoById(Integer BrandId) throws Exception {
		mdd.selectCollection("ProductBrandInfo");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"BrandId\":" + BrandId + "}", 1);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			Logger.debug("err data BrandId:" + BrandId);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 查询记录
	 * 
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductBrandInfos(Integer limit) throws Exception {
		mdd.selectCollection("ProductBrandInfo");
		if (limit < 1) {
			limit = 1;
		}
		ArrayList<Map> list = mdd.selectPartLimitToMap("{}", limit);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 按CategoryId查询品类信息
	 * @param CategoryId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductCatalogInfoByCid(String CategoryId) throws Exception {
		mdd.selectCollection("ProductCategoryInfo");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"CategoryId\":" + CategoryId + "}",1);
		Logger.debug(list);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void deleteProductCatalogInfoByCid(Integer CategoryId) throws Exception {
		if(CategoryId==null) {
			return;
		}
		mdd.selectCollection("ProductCategoryInfo");
		mdd.delete("{\"CategoryId\":" + CategoryId + "}");
	}
	/**
	 * 查询品类信息
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductCategoryInfoLimit(int limit) throws Exception {
    	if(limit<1){
    		limit=1;
    	}
    	mdd.selectCollection("ProductCategoryInfo");
        Map<String, Object> scmb = new HashMap<String, Object>();
        ArrayList<Map> list = mdd.selectPartLimitToMap("{}",limit);
             try {
            if (list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	public static void main(String args[]) throws Exception {
		new BuyerProductWapper().deleteProductCatalogInfoByCid(10088);
	}
}
